#!/bin/bash

# 设置主机名
hostnamectl set-hostname 'k8s-master'
exec bash
echo '192.168.2.218 k8s-master' >> /etc/hosts

# 禁用SELinux
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

# 开放防火墙
firewall-cmd --zone=public --add-port=6443/tcp --permanent
firewall-cmd --zone=public --add-port=10250/tcp --permanent
firewall-cmd --reload

# 设置iptables
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

# 禁用swap
swapoff -a
sed -i '/swap/d' /etc/fstab
echo 'swapoff -a ' >> /etc/rc.d/rc.local

# 设置Kubernetes仓库
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 设置docker仓库
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 更新yum
yum update -y

# 安装docker
yum install -y docker
systemctl restart docker && systemctl enable docker

# 设置镜像仓库
cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://registry.docker-cn.com/",
    "https://hub.daocloud.io/",
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com"
  ]
}
EOF

systemctl daemon-reload
systemctl restart docker

#安装kubernetes
yum install -y kubeadm-1.18.8-0 kubectl-1.18.8-0 kubelet-1.18.8-0 kubernetes-cni-0.8.6-0
systemctl restart kubelet && systemctl enable kubelet

# 初始化master节点
kubeadm init --kubernetes-version=v1.18.0 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.2.218
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 初始化网络
echo '199.232.68.133 raw.githubusercontent.com' >> /etc/hosts
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

# 允许master调度
kubectl taint nodes --all node-role.kubernetes.io/master-

# 部署MySQL
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/mysql.yaml

# 部署MongoDB
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/mongo.yaml

# 部署redis
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/redis.yaml

# 部署Kafka + zookeeper
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/kafka.yaml

# 部署服务端
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/aieyes-server.yaml

# 部署前端
kubectl apply -f https://gitee.com/liweitaos/aieyes-deploy/raw/master/aieyes-web.yaml

# # 安装JDK
# yum install -y java-1.8.0-openjdk*
# cat <<EOF >> /etc/profile
# export JAVA_HOME=/etc/alternatives/java_sdk_1.8.0
# export PATH=$JAVA_HOME/bin:$PATH
# EOF

# # 安装maven
# yum install -y wget
# cd ~
# wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# tar xf apache-maven-3.6.3-bin.tar.gz  -C /usr/local/
# ln -s /usr/local/apache-maven-3.6.3 /usr/local/maven
# ln -s /usr/local/maven/bin/mvn /usr/local/bin/mvn

# # 安装git
# yum install -y git

# # 拉取后端代码
# cd ~
# git clone http://tools.microcental.net:82/aieyes/aieyes.git
# cd aieyes
# # 编译&打包
# mvn clean install -Dmaven.test.skip=true

# # 构建镜像
# cd aieyes-starter
# docker build -t aieyes-server:latest -f Dockerfile .

# # 部署到k8s
# cd ~/aieyes-deploy
# kubectl apply -f aieyes-server.yaml


# # 安装nodejs
# curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash -
# yum install -y nodejs

# # 拉取前端代码
# cd ~
# git clone http://tools.microcental.net:82/aieyes/aieyes-web.git
# cd aieyes-web

# # 编译&构建
# npm install --registry=https://registry.npm.taobao.org
# npm run build:prod

# # 构建镜像
# docker build -t aieyes-web:latest -f Dockerfile .

# # 部署到k8s
# cd ~/aieyes-deploy
# kubectl apply -f aieyes-web.yaml

